package com.neuedu.his.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.his.mapper.CheckapplyMapper;
import com.neuedu.his.mapper.FmeditemMapper;
import com.neuedu.his.po.Checkapply;
import com.neuedu.his.po.Fmeditem;
import com.neuedu.his.service.CheckapplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CheckapplyServiceImpl extends ServiceImpl<CheckapplyMapper, Checkapply> implements CheckapplyService {

    @Autowired
    private CheckapplyMapper checkapplyMapper;

    @Autowired
    private FmeditemMapper fmeditemMapper;

    @Override
    public boolean createCheckApply(Integer medicalRecordId, Integer registerId, Integer doctorId,
                                    List<Integer> itemIds, List<String> objectives,
                                    List<String> positions, List<Integer> isUrgents,
                                    List<Integer> nums) {
        if (itemIds == null || itemIds.isEmpty() ||
                itemIds.size() != objectives.size() ||
                itemIds.size() != positions.size() ||
                itemIds.size() != isUrgents.size() ||
                itemIds.size() != nums.size()) {
            throw new IllegalArgumentException("参数列表长度不一致");
        }

        for (int i = 0; i < itemIds.size(); i++) {
            Integer itemId = itemIds.get(i);
            Fmeditem fmeditem = fmeditemMapper.selectById(itemId);
            if (fmeditem == null) {
                throw new RuntimeException("未找到ID为" + itemId + "的检查项目");
            }

            Checkapply checkapply = new Checkapply();
            checkapply.setMedicalid(medicalRecordId);
            checkapply.setRegistid(registerId);
            checkapply.setItemid(itemId);
            checkapply.setName(fmeditem.getItemname());
            checkapply.setObjective(objectives.get(i));
            checkapply.setPosition(positions.get(i));
            checkapply.setIsurgent(isUrgents.get(i));
            checkapply.setNum(nums.get(i));
            checkapply.setCreationtime(new Date());
            checkapply.setDoctorid(doctorId);
            checkapply.setState(1); // 状态默认为1-已开立
            checkapply.setRecordtype(fmeditem.getRecordtype());

            // 设置默认检查人员和结果录入人员(可根据实际业务调整)
            checkapply.setCheckoperid(doctorId);
            checkapply.setResultoperid(doctorId);

            checkapplyMapper.insert(checkapply);
        }
        return true;
    }

    @Override
    public List<Checkapply> getPendingChecks() {
        return lambdaQuery()
                .eq(Checkapply::getState, 2) // 状态为2-已支付
                .orderByAsc(Checkapply::getIsurgent) // 加急的排在前面
                .orderByAsc(Checkapply::getCreationtime) // 按创建时间排序
                .list();
    }

    @Override
    public List<Checkapply> getPendingChecksByDept(Integer deptId) {
        if (deptId == null) {
            throw new IllegalArgumentException("科室ID不能为空");
        }
        return lambdaQuery()
                .eq(Checkapply::getState, 2) // 状态为2-已支付
                .inSql(Checkapply::getItemid,
                        "SELECT ID FROM fmeditem WHERE DeptID = " + deptId) // 属于该科室的项目
                .orderByAsc(Checkapply::getIsurgent) // 加急的排在前面
                .orderByAsc(Checkapply::getCreationtime) // 按创建时间排序
                .list();
    }

    @Override
    public boolean updateState(List<Integer> checkApplyIds, Integer state) {
        if (checkApplyIds == null || checkApplyIds.isEmpty() || state == null) {
            return false;
        }

        return lambdaUpdate()
                .in(Checkapply::getId, checkApplyIds)
                .set(Checkapply::getState, state)
                .update();
    }

    @Override
    public boolean startCheck(Integer checkApplyId, Integer operatorId) {
        if (checkApplyId == null || operatorId == null) {
            return false;
        }

        return lambdaUpdate()
                .eq(Checkapply::getId, checkApplyId)
                .set(Checkapply::getState, 3) // 3-检查中
                .set(Checkapply::getCheckoperid, operatorId)
                .set(Checkapply::getChecktime, new Date())
                .update();
    }

    @Override
    public List<Checkapply> getAllChecks() {
        return lambdaQuery()
                .orderByAsc(Checkapply::getIsurgent) // 加急的排在前面
                .orderByAsc(Checkapply::getCreationtime) // 按创建时间排序
                .list();
    }

    @Override
    public List<Checkapply> getChecksByDept(Integer deptId) {
        if (deptId == null) {
            throw new IllegalArgumentException("科室ID不能为空");
        }
        return lambdaQuery()
                .inSql(Checkapply::getItemid,
                        "SELECT ID FROM fmeditem WHERE DeptID = " + deptId) // 属于该科室的项目
                .orderByAsc(Checkapply::getIsurgent) // 加急的排在前面
                .orderByAsc(Checkapply::getCreationtime) // 按创建时间排序
                .list();
    }


    @Override
    public List<Checkapply> getAllChecksWithPatientName() {
        return checkapplyMapper.selectAllChecksWithPatientName();
    }

    @Override
    public List<Checkapply> getChecksByDeptWithPatientName(Integer deptId) {
        if (deptId == null) {
            throw new IllegalArgumentException("科室ID不能为空");
        }
        return checkapplyMapper.selectChecksByDeptWithPatientName(deptId);
    }
}